﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Repositories;
using DomenModel;
using System.Data.Entity;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;

namespace Infrastructure.Repositories
{
    public class OrderDetailsRepo : IOrderDetailsCrud
    {

        /// <summary>
        /// Gets the OrderDetails by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        public OrderDetails GetById(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                OrderDetails orderDetailsById = _db.OrderDetailses.FirstOrDefault(p => p.OrderDetailsId == Id);
                return orderDetailsById;
            }

        }

        /// <summary>
        /// Saves the OrderDetails to data base.
        /// </summary>
        /// <param name="t">The t.</param>
        public void Save(OrderDetails t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                _db.OrderDetailses.Add(t);
                _db.SaveChanges();
            }

        }

        /// <summary>
        /// Updates the OrderDetails.
        /// </summary>
        /// <param name="t">The t.</param>
        public void Update(OrderDetails t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                OrderDetails orderDetailsById = _db.OrderDetailses.FirstOrDefault(p => p.OrderDetailsId == t.OrderDetailsId);
                orderDetailsById.ItemDimensionId = t.ItemDimensionId;
                orderDetailsById.ItemDimension = t.ItemDimension;
                orderDetailsById.OrderId = t.OrderId;
                orderDetailsById.Order = t.Order;
                orderDetailsById.Count = t.Count;
                orderDetailsById.Price = t.Price;
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the OrderDetails from data base by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        public void Delete(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                OrderDetails orderDetailsById = _db.OrderDetailses.FirstOrDefault(p => p.OrderDetailsId == Id);
                _db.OrderDetailses.Remove(orderDetailsById);
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Gets the collection of all OrderDetails.
        /// </summary>
        /// <returns></returns>
        public List<OrderDetails> GetCollection()
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var listOfOrderDetails = _db.OrderDetailses.ToList();
                return listOfOrderDetails;
            }
        }

        /// <summary>
        /// Gets the collection of OrderDetails by id of Order.
        /// </summary>
        /// <param name="orderId">The order id.</param>
        /// <returns></returns>
        public List<OrderDetails> GetCollection(int orderId)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var orderDetailsByFilter = _db.OrderDetailses.AsQueryable<OrderDetails>()
                    .Include("ItemDimension")
                    .Include("ItemDimension.Item")
                    .Where(p => p.OrderId == orderId)
                    .ToList();
                return orderDetailsByFilter;
            }
        }

        /// <summary>
        /// Gets the collection of OrderDetails by user's name.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public List<OrderDetails> GetCollection(string userName)
        {
            using (GeneralContext _db = new GeneralContext())
            {

                    var user = _db.Users.FirstOrDefault(a => a.LoginName == userName);
                    var listOfOrderDetails = _db.OrderDetailses
                        .Include("ItemDimension")
                        .Include("ItemDimension.Item")
                        .Include("ItemDimension.Dimension")
                        .Where(p => p.Order.UserId == user.UserId)
                        .ToList();
                    return listOfOrderDetails;

            }
        }

        /// <summary>
        /// Gets the count of OrderDetails by id of Order.
        /// </summary>
        /// <param name="orderId">The order id.</param>
        /// <returns></returns>
        public long GetCount(int orderId)
        {

            using (GeneralContext _db = new GeneralContext())
            {
                long ordersDetailsById = _db.OrderDetailses.Where(p => p.OrderId == orderId).Count();
                return ordersDetailsById;
            }

        }

        /// <summary>
        /// Gets the list of Items for pagination by condition.
        /// </summary>
        /// <param name="page">The page id.</param>
        /// <param name="itemsOnPage">The items on page.</param>
        /// <param name="descending">if set to <c>true</c> [descending].</param>
        /// <param name="orderId">The order id.</param>
        /// <returns></returns>
        public List<OrderDetails> GetListForPagination(int page, int itemsOnPage, bool descending, int orderId)
        {
            using (GeneralContext _db = new GeneralContext())
            {

                    if (descending == true)
                    {
                        var listForPagination = _db.OrderDetailses
                             .Include("ItemDimension")
                             .Include("ItemDimension.Item")
                            .Where(p => p.OrderId == orderId)
                            .Skip((page - 1) * itemsOnPage)
                            .Take(itemsOnPage)

                            .ToList();

                        return listForPagination;
                    }
                    else
                    {
                        var listForPagination = _db.OrderDetailses
                            .Include("ItemDimension")
                            .Include("ItemDimension.Item")
                            .OrderBy(c => c.OrderDetailsId)
                            .Where(p => p.OrderId == orderId)
                            .Skip((page - 1) * itemsOnPage)
                            .Take(itemsOnPage)
                            .ToList();

                        return listForPagination;
                    }

            }
        }

    }
}
